﻿using Microsoft.AspNetCore.Mvc;
using SiteApp.BLL.Common;
using SiteApp.BLL.Interface;
using SiteApp.Model;
using SiteApp.Web.Model;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace SiteApp.Web.Controllers
{
    [ApiController]
    [Route("[controller]")]
    /// <summary>
    /// 与设备相关接口
    /// </summary>
    /// <returns></returns>
    public class PointController : ControllerBase
    {
        private bool IsValid()
        {
            if (HttpContext.User.Claims.ToList() != null && HttpContext.User.Claims.ToList()[0].Type == "IsAuthenticated" && HttpContext.User.Claims.ToList()[0].Value == "true")
            {
                return true;
            }
            return false;
        }

        private readonly IPointService pointService = null;
        private readonly INewInfraredService newInfrared = null;

        public PointController(
             IPointService point,
             INewInfraredService _newInfrared)
        {

            this.pointService = point;
            this.newInfrared = _newInfrared;
        }

        /// <summary>
        /// 模糊匹配查询设备
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("SearchPoint")]
        public async Task<JsonResult> SearchPoint([FromForm] string type, [FromForm] string siteId, [FromForm] string voltage, [FromForm] string device)
        {
            var result = new WebResult();

            if (IsValid())
            {
                result.Data = pointService.SearchPoint(type, siteId, voltage, device);
            }
            return new JsonResult(result);
        }

        /// <summary>
        /// 设备根据时间查询数据   type 1 小时 2 天  3 月 4年   pointType 1 温湿度传感器  2 sf6传感器  3 老红外传感器 4 新红外传感器
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("PointForDate")]
        public async Task<JsonResult> PointForDate([FromForm] string pointId, [FromForm] string startTime, [FromForm] string endTime, [FromForm] int type, [FromForm] int pointType)
        {

            var result = new WebResult();

            if (IsValid())
            {
                var pointEntity = pointService.GetPoint(pointId);//新老红外在库里区分
                if (pointEntity!=null)
                {
                    pointType = pointEntity.Type;

                    if (pointType != 4)
                    {
                        var list = pointService.PointForDate(pointId, startTime, endTime, pointType.ToString());
                        list = list.Distinct(new ComparerHelper<PointDetails>((x, y) => (null != x && null != y) && (x.CREATETIME == y.CREATETIME))).ToList();

                        var chart = new WebChart();
                        switch (type)
                        {
                            case 1://按小时的 三个传感器
                                if (pointType == 1)//温湿度传感器 按小时分
                                {
                                    chart.Temperature = new ChartArr();
                                    chart.Temperature.XArr = new List<string>();
                                    chart.Temperature.YArr = new List<string>();

                                    chart.Humidity = new ChartArr();
                                    chart.Humidity.XArr = new List<string>();
                                    chart.Humidity.YArr = new List<string>();

                                    for (int i = 0; i < list.Count; i++)
                                    {
                                        chart.Temperature.XArr.Add(list[i].CREATETIME.ToString());
                                        chart.Temperature.YArr.Add(list[i].Temperature.ToString());

                                        chart.Humidity.XArr.Add(list[i].CREATETIME.ToString());
                                        chart.Humidity.YArr.Add(list[i].Humidity.ToString());
                                    }
                                }
                                if (pointType == 2)//SF6传感器 按小时分
                                {
                                    list = list.Distinct(new ComparerHelper<PointDetails>((x, y) => (null != x && null != y) && (x.CREATETIME == y.CREATETIME))).ToList();


                                    chart.Moisture = new ChartArr();
                                    chart.Moisture.XArr = new List<string>();
                                    chart.Moisture.YArr = new List<string>();

                                    chart.Pressure = new ChartArr();
                                    chart.Pressure.XArr = new List<string>();
                                    chart.Pressure.YArr = new List<string>();

                                    for (int i = 0; i < list.Count; i++)
                                    {
                                        chart.Moisture.XArr.Add(list[i].CREATETIME.ToString());
                                        chart.Moisture.YArr.Add(list[i].Moisture.ToString());

                                        chart.Pressure.XArr.Add(list[i].CREATETIME.ToString());
                                        chart.Pressure.YArr.Add(list[i].Pressure.ToString());
                                    }

                                }
                                if (pointType == 3)//红外传感器 按小时分
                                {
                                    list = list.Distinct(new ComparerHelper<PointDetails>((x, y) => (null != x && null != y) && (x.CREATETIME == y.CREATETIME))).ToList();


                                    chart.Infrared = new List<Infrared>();


                                    for (int i = 0; i < list.Count; i++)
                                    {
                                        chart.Infrared.Add(new Infrared
                                        {
                                            Img = list[i].Img.ToString(),
                                            Time = list[i].CREATETIME.ToString("yyyy-MM-dd HH:mm:ss")

                                        });

                                    }
                                }


                                break;
                            case 2://按天的 三个传感器
                                var dayList = list.GroupBy(x => x.CREATETIME.ToString("yyyy/MM/dd")).Select(x => new { Day = x.Key, List = x.ToList() });

                                if (pointType == 1)
                                {
                                    chart.Temperature = new ChartArr();
                                    chart.Temperature.XArr = new List<string>();
                                    chart.Temperature.YArr = new List<string>();

                                    chart.Humidity = new ChartArr();
                                    chart.Humidity.XArr = new List<string>();
                                    chart.Humidity.YArr = new List<string>();

                                    foreach (var item in dayList)
                                    {
                                        var model = item.List.OrderByDescending(a => a.CREATETIME).ToList() != null && item.List.OrderByDescending(a => a.CREATETIME).ToList().Count > 0
                                            ? item.List.OrderByDescending(a => a.CREATETIME).ToList()[0] : new PointDetails();

                                        chart.Temperature.XArr.Add(item.Day);
                                        chart.Temperature.YArr.Add(model.Temperature);

                                        chart.Humidity.XArr.Add(item.Day);
                                        chart.Humidity.YArr.Add(model.Humidity);
                                    }
                                }

                                if (pointType == 2)
                                {
                                    chart.Pressure = new ChartArr();
                                    chart.Pressure.XArr = new List<string>();
                                    chart.Pressure.YArr = new List<string>();

                                    chart.Moisture = new ChartArr();
                                    chart.Moisture.XArr = new List<string>();
                                    chart.Moisture.YArr = new List<string>();

                                    foreach (var item in dayList)
                                    {
                                        var model = item.List.OrderByDescending(a => a.CREATETIME).ToList() != null && item.List.OrderByDescending(a => a.CREATETIME).ToList().Count > 0
                                            ? item.List.OrderByDescending(a => a.CREATETIME).ToList()[0] : new PointDetails();

                                        chart.Pressure.XArr.Add(item.Day);
                                        chart.Pressure.YArr.Add(model.Pressure);

                                        chart.Moisture.XArr.Add(item.Day);
                                        chart.Moisture.YArr.Add(model.Moisture);
                                    }
                                }

                                if (pointType == 3)
                                {
                                    chart.Infrared = new List<Infrared>();

                                    foreach (var item in dayList)
                                    {
                                        var model = item.List.OrderByDescending(a => a.CREATETIME).ToList() != null && item.List.OrderByDescending(a => a.CREATETIME).ToList().Count > 0
                                            ? item.List.OrderByDescending(a => a.CREATETIME).ToList()[0] : new PointDetails();

                                        chart.Infrared.Add(new Infrared()
                                        {
                                            Img = model.Img,
                                            Time = model.CREATETIME.ToString("yyyy-MM-dd HH:mm:ss")

                                        });


                                    }
                                }

                                break;
                            case 3://按月的 三个传感器

                                var monthList = list.GroupBy(x => x.CREATETIME.ToString("yyyy/MM")).Select(x => new { month = x.Key, List = x.ToList() });

                                if (pointType == 1)
                                {
                                    chart.Temperature = new ChartArr();
                                    chart.Temperature.XArr = new List<string>();
                                    chart.Temperature.YArr = new List<string>();

                                    chart.Humidity = new ChartArr();
                                    chart.Humidity.XArr = new List<string>();
                                    chart.Humidity.YArr = new List<string>();

                                    foreach (var item in monthList)
                                    {
                                        var model = item.List.OrderByDescending(a => a.CREATETIME).ToList() != null && item.List.OrderByDescending(a => a.CREATETIME).ToList().Count > 0
                                            ? item.List.OrderByDescending(a => a.CREATETIME).ToList()[0] : new PointDetails();

                                        chart.Temperature.XArr.Add(item.month);
                                        chart.Temperature.YArr.Add(model.Temperature);

                                        chart.Humidity.XArr.Add(item.month);
                                        chart.Humidity.YArr.Add(model.Humidity);
                                    }
                                }

                                if (pointType == 2)
                                {
                                    chart.Pressure = new ChartArr();
                                    chart.Pressure.XArr = new List<string>();
                                    chart.Pressure.YArr = new List<string>();

                                    chart.Moisture = new ChartArr();
                                    chart.Moisture.XArr = new List<string>();
                                    chart.Moisture.YArr = new List<string>();

                                    foreach (var item in monthList)
                                    {
                                        var model = item.List.OrderByDescending(a => a.CREATETIME).ToList() != null && item.List.OrderByDescending(a => a.CREATETIME).ToList().Count > 0
                                            ? item.List.OrderByDescending(a => a.CREATETIME).ToList()[0] : new PointDetails();

                                        chart.Pressure.XArr.Add(item.month);
                                        chart.Pressure.YArr.Add(model.Pressure);

                                        chart.Moisture.XArr.Add(item.month);
                                        chart.Moisture.YArr.Add(model.Moisture);
                                    }
                                }

                                if (pointType == 3)
                                {
                                    chart.Infrared = new List<Infrared>();

                                    foreach (var item in monthList)
                                    {
                                        var model = item.List.OrderByDescending(a => a.CREATETIME).ToList() != null && item.List.OrderByDescending(a => a.CREATETIME).ToList().Count > 0
                                            ? item.List.OrderByDescending(a => a.CREATETIME).ToList()[0] : new PointDetails();

                                        chart.Infrared.Add(new Infrared()
                                        {
                                            Img = model.Img,
                                            Time = model.CREATETIME.ToString("yyyy-MM-dd HH:mm:ss")

                                        });


                                    }
                                }

                                break;

                            case 4://按年的 三个传感器

                                var yearList = list.GroupBy(x => x.CREATETIME.ToString("yyyy")).Select(x => new { year = x.Key, List = x.ToList() });

                                if (pointType == 1)
                                {
                                    chart.Temperature = new ChartArr();
                                    chart.Temperature.XArr = new List<string>();
                                    chart.Temperature.YArr = new List<string>();

                                    chart.Humidity = new ChartArr();
                                    chart.Humidity.XArr = new List<string>();
                                    chart.Humidity.YArr = new List<string>();

                                    foreach (var item in yearList)
                                    {
                                        var model = item.List.OrderByDescending(a => a.CREATETIME).ToList() != null && item.List.OrderByDescending(a => a.CREATETIME).ToList().Count > 0
                                            ? item.List.OrderByDescending(a => a.CREATETIME).ToList()[0] : new PointDetails();

                                        chart.Temperature.XArr.Add(item.year);
                                        chart.Temperature.YArr.Add(model.Temperature);

                                        chart.Humidity.XArr.Add(item.year);
                                        chart.Humidity.YArr.Add(model.Humidity);
                                    }
                                }

                                if (pointType == 2)
                                {
                                    chart.Pressure = new ChartArr();
                                    chart.Pressure.XArr = new List<string>();
                                    chart.Pressure.YArr = new List<string>();

                                    chart.Moisture = new ChartArr();
                                    chart.Moisture.XArr = new List<string>();
                                    chart.Moisture.YArr = new List<string>();

                                    foreach (var item in yearList)
                                    {
                                        var model = item.List.OrderByDescending(a => a.CREATETIME).ToList() != null && item.List.OrderByDescending(a => a.CREATETIME).ToList().Count > 0
                                            ? item.List.OrderByDescending(a => a.CREATETIME).ToList()[0] : new PointDetails();

                                        chart.Pressure.XArr.Add(item.year);
                                        chart.Pressure.YArr.Add(model.Pressure);

                                        chart.Moisture.XArr.Add(item.year);
                                        chart.Moisture.YArr.Add(model.Moisture);
                                    }
                                }

                                if (pointType == 3)
                                {
                                    chart.Infrared = new List<Infrared>();

                                    foreach (var item in yearList)
                                    {
                                        var model = item.List.OrderByDescending(a => a.CREATETIME).ToList() != null && item.List.OrderByDescending(a => a.CREATETIME).ToList().Count > 0
                                            ? item.List.OrderByDescending(a => a.CREATETIME).ToList()[0] : new PointDetails();

                                        chart.Infrared.Add(new Infrared()
                                        {
                                            Img = model.Img,
                                            Time = model.CREATETIME.ToString("yyyy-MM-dd HH:mm:ss")

                                        });


                                    }
                                }

                                break;

                        }
                        result.Data = chart;
                    }

                    if (pointType == 4)
                    {
                        result.Data = newInfrared.NewInfrareForDate(pointId, startTime, endTime);
                    }
                }
              

            }



            return new JsonResult(result);
        }

        [HttpPost]
        [Route("NewInfrared")]
        public async Task<JsonResult> NewInfrared()
        {

         
            Bitmap bmpTemp = null;
            using (StreamReader strm = new StreamReader(@"C:\Users\newYear\Desktop\4019510767119237120.jpg"))
            {

                Image img = Image.FromStream(strm.BaseStream);

                Bitmap bmp = new Bitmap(img);

                #region  图片加字



                //Graphics g = Graphics.FromImage(bmp);
                //String str = "hello, string";
                //Font font = new Font("宋体", 8);
                //SolidBrush sbrush = new SolidBrush(Color.Black);
                //g.DrawString(str, font, sbrush, new PointF(10, 10));
                //MemoryStream ms = new MemoryStream();

                //bmpTemp = new Bitmap(bmp);
                //img.Dispose();
                //bmp.Dispose();


                #endregion

                #region 矩形

                //Graphics g = Graphics.FromImage(bmp);

                //Brush brush = new SolidBrush(Color.Red);
                //Pen pen = new Pen(brush, 1);
                //pen.DashStyle = DashStyle.DashDot;

                //g.DrawRectangle(pen, new Rectangle(0, 0, 100, 250));

                //g.Dispose();


                //img.Dispose();
                //bmpTemp = new Bitmap(bmp);
                //bmp.Dispose();

                #endregion

                #region 圆形

                //Graphics g = Graphics.FromImage(bmp);

                //Brush brush = new SolidBrush(Color.Red);
                //Pen pen = new Pen(brush, 1);
                //pen.DashStyle = DashStyle.DashDot;

                //g.DrawEllipse(pen, new Rectangle(50, 50, 100, 250));

                //g.Dispose();


                //img.Dispose();
                //bmpTemp = new Bitmap(bmp);
                //bmp.Dispose();

                #endregion

                #region 线

                //Graphics g = Graphics.FromImage(bmp);

                //Brush brush = new SolidBrush(Color.Black);

                //Pen pen = new Pen(brush, 1);
                ////pen.Alignment = PenAlignment.Inset;

                //pen.DashStyle = DashStyle.DashDot;

                //g.DrawLine(pen, 200, 200, 300, 300);

                //g.Dispose();

                //img.Dispose();
                //bmpTemp = new Bitmap(bmp);
                //bmp.Dispose();

                #endregion

                #region 图片任意像素点

                bmpTemp = ImageHelper.ImaDrawPixel(bmp,0,0,255,255,255); 

                #endregion





            }

            bmpTemp.Save(@"C:\Users\newYear\Desktop\4019510767119237120.jpg", ImageFormat.Jpeg);


            return null;
        }


    }
}
